package org.kernely.ui.admin;

import org.kernely.ui.MainApplication;
import org.kernely.ui.mvp.IKernelyView;
import org.kernely.ui.mvp.KernelyEventSender;

import com.vaadin.terminal.ThemeResource;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Component;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Panel;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.themes.Reindeer;

/**
 *	Administration view of Kernely, where bundles with administration panel are displayed.
 */
public class KernelyAdminView extends GridLayout implements IKernelyView, KernelyEventSender {

	/**
	 * The Serial Version ID
	 */
	private static final long serialVersionUID = 6368552942552952939L;

	private VerticalLayout bundlesLayout;
	private VerticalLayout bundleAdminPageLayoutContent;
	private Label bundleAdminPageLabel;

	/**
	 * KernelyAdminView's constructor
	 */
	public KernelyAdminView(){
		
		Label title = new Label("Administration panel");
		title.setSizeUndefined();
		title.setStyleName(Reindeer.LABEL_H1);
		title.setIcon(new ThemeResource("img/admin32.png"));
		VerticalLayout vlayout = new VerticalLayout();
		Panel headerAdmin = new Panel();
		headerAdmin.setContent(vlayout);
		vlayout.addComponent(title);
		vlayout.setMargin(true, false, true, false);
		vlayout.setComponentAlignment(title, Alignment.MIDDLE_CENTER);
		headerAdmin.setSizeFull();
		
		Panel bundlesPanel = new Panel();
		bundlesLayout = new VerticalLayout();
		bundlesLayout.setSpacing(true);
		bundlesPanel.addComponent(bundlesLayout);
//		bundlesLayout.setMargin(true, true, true, true);
		bundlesPanel.setSizeFull();
		
		Panel bundleAdminPage = new Panel();
		VerticalLayout bundleAdminPageLayout = new VerticalLayout();
		bundleAdminPage.setContent(bundleAdminPageLayout);
		
		bundleAdminPageLabel = new Label("Here you can administrate installed bundles. Click on the icon of the bundle you want to administrate to open it's administration panel.");
		bundleAdminPageLabel.setSizeUndefined();
		
		bundleAdminPageLayout.addComponent(bundleAdminPageLabel);
		bundleAdminPageLayout.addComponent(new Label("<hr/>", Label.CONTENT_XHTML));
		bundleAdminPageLayoutContent = new VerticalLayout();
		bundleAdminPageLayout.addComponent(bundleAdminPageLayoutContent);
		bundleAdminPageLayout.setComponentAlignment(bundleAdminPageLabel, Alignment.MIDDLE_CENTER);
		bundleAdminPageLayout.setMargin(true, true, true, true);
		
		bundleAdminPage.setSizeFull();
		
		this.setRows(2);
		this.setColumns(6);
		this.addComponent(headerAdmin, 0, 0, 5, 0);
		this.addComponent(bundlesPanel, 0, 1, 0, 1);
		this.addComponent(bundleAdminPage, 1, 1, 5, 1);
		
		this.setSizeFull();
	}
	
	/**
	 * @see org.kernely.core.service.IKernelyView#getComponent()
	 */
	public final Component getComponent() {
		return this;
	}

	/**
	 * This method is called when this component is added to an other.
	 */
	@Override
	public final void attach(){
		super.attach();
		//When the component is attached, we fire listeners
		MainApplication.getEventBus().broadcastEvent(new KernelyAdminAttachedEvent(this));
	}

	/**
	 * Gets the bundlesPanel, where bundles are displayed
	 * @return the bundlesPanel
	 */
	public final VerticalLayout getBundlesLayout() {
		return bundlesLayout;
	}

	public final VerticalLayout getBundleAdminPageLayoutContent(){
		return bundleAdminPageLayoutContent;
	}
	
	public final Label getBundleAdminPageLabel(){
		return bundleAdminPageLabel;
	}
}
